<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>

    <script src="js/jquery-1.11.0.js"></script>
</head>
<body>
    <div>
        <input type="hidden" name="id" id="aid">

        name: <input type="text" name="name" id="name"> <br>
        money: <input type="text" name="money" id="money"> <br>


        <button id="update">更新</button>
    </div>

    <script>
        //发送异步请求，加载当前账户id对应的数据进行回显
        let search = location.search;  // ?id=2
        let aid = search.split("=")[1];

        $.ajax({
            url:"/account/"+aid,
            type:"get",
            success:function (r){
                let code = r.code;
                if (code == 200){
                    //进行数据回显
                    let account = r.data;
                    $("#aid").val(account.id);
                    $("#name").val(account.name);
                    $("#money").val(account.money);
                }else{
                    //失败
                    alert(r.msg)
                }
            },
            dataType:"json"
        })


        //绑定更新按钮的单击事件
        $("#update").click(function (){
            //获取最新的数据
            let id = $("#aid").val();
            let name = $("#name").val();
            let money = $("#money").val();

            let account = {"id":id,"name":name,"money":money}

            //发送异步请求，执行更新操作
            $.ajax({
                url:"/account",
                type:"put",
                data:JSON.stringify(account),
                contentType:"application/json",
                success:function (r){
                    let code = r.code;
                    if (code == 200){
                        //更新成功，跳转到首页
                        location.href = "index.html"
                    }else{
                        //更新失败，提示用户
                        alert(r.msg)
                    }
                },
                dataType: "json"
            })
        });
    </script>
</body>
</html>